package com.example.q_kang.hhh.setting;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.login.LoginActivity;
import com.hjq.toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.sskj.base.MVPActivity;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.HideUtil;
import com.sskj.base.utils.SPUtils;
import com.sskj.base.view.ToolBarLayout;
import com.yalantis.ucrop.util.FastBitmapDrawable;

import java.lang.reflect.MalformedParameterizedTypeException;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.internal.operators.flowable.FlowableLastMaybe;

/**
 * @author Hey
 * Create at  2019/04/10
 */
public class SetLoginPwdActivity extends MVPActivity<SetLoginPwdPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_logi_npwd;
    }

    @Override
    public SetLoginPwdPresenter getPresenter() {
        return new SetLoginPwdPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        tvMobile.setText(HideUtil.getPhoneHide((String) SPUtils.get(App.INSTANCE, SpConfig.MOBILE, "")));
        ClickUtil.click(300, tvGetCode, view -> mPresenter.sendCode((String) SPUtils.get(App.INSTANCE, SpConfig.MOBILE, "")));
        ClickUtil.click(300, btnSubmit, view -> {
            if (TextUtils.isEmpty(etPwd.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetLoginPwdActivity3));
                return;
            }
            if (etPwd.getText().length() < 6) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetLoginPwdActivity4));
                return;
            }
            if (!etPwd.getText().toString().equals(etConfirmPwd.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetLoginPwdActivity5));
                return;
            }
            if (TextUtils.isEmpty(etCode.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetLoginPwdActivity6));
                return;
            }
            showLoading();
            mPresenter.resetLoginPwd(etPwd.getText().toString(),
                    etCode.getText().toString());
        });
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SetLoginPwdActivity.class);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void resetSuccess() {
        HttpHeaders commonHeaders = OkGo.getInstance().getCommonHeaders();
        HttpParams commonParams = OkGo.getInstance().getCommonParams();
        commonHeaders.clear();
        commonParams.clear();
        commonHeaders.put("systemType", "ANDROID");
        commonParams.put("systemType", "ANDROID");
        OkGo.getInstance().addCommonHeaders(commonHeaders);
        OkGo.getInstance().addCommonParams(commonParams);
        LoginActivity.start(this);
        finish();
    }
}
